﻿using System;
using FlowCanvas;
using FlowCanvas.Nodes;

namespace Yoozoo.Gameplay.Liberty.StreetEvent
{
    public class OpenStreetInteractivePanel : CallableActionNodeBase
    {
        public static Action<int> OnInteractiveAction;

        public static int InteractiveUniqueId = 1;
        
        private int _interactiveUniqueId;

        public FlowOutput onClick;

        private void Invoke(string langKey,string iconPath)
        {
            _interactiveUniqueId = InteractiveUniqueId;
            InteractiveUniqueId++;

            BlueprintUtils.OpenStreetInteractivePanel(_interactiveUniqueId, langKey, iconPath);
        }

        public override void OnGraphStarted()
        {
            OnInteractiveAction += OnInteractiveActionFunc;
        }

        private void OnInteractiveActionFunc(int actionId)
        {
            if (actionId == _interactiveUniqueId)
            {
                onClick?.Call(new Flow());
            }
        }

        public override void OnGraphStoped()
        {
            OnInteractiveAction -= OnInteractiveActionFunc;
        }

        protected override void OnRegisterPorts(FlowNode node)
        {
            var p1 = node.AddValueInput<string>("langKey");
            var p2 = node.AddValueInput<string>("iconPath");
            var o = node.AddFlowOutput("Out");
            node.AddFlowInput("In", (f) =>
            {
                Invoke(p1.value,p2.value);
                o.Call(f);
            });
            
            onClick = node.AddFlowOutput("onClick");
        }
    }
}